home *** CD-ROM | disk | FTP | other *** search
- Message-ID: <311693FD.6BBF@novell.com>
- Date: Mon, 05 Feb 1996 16:34:21 -0700
- From: Sukanta Ganguly <sukanta_ganguly@novell.com>
- Organization: Novell Inc
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
- MIME-Version: 1.0
- Newsgroups: comp.lang.c++
- Subject: Re: Question on constructors
- References: <3103E642.41C6@bme.jhu.edu>
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- NNTP-Posting-Host: sganguly.npd.provo.novell.com
- Path: news.provo.novell.com!
-
- Stephanos Androutsellis-Theotokis wrote:
- >
- > I have the following question on constructors:
- >
- > I wrote a c++ library that uses a dummy non-local static instance of a class
- > with a default constructor to force library initialization.
- > I thought C++ semantics guaranteed that the constructor for this dummy
- > object is called before any other functions in the same "translation unit",
- > or, at the very least, before any member functions of this dummy object
- > are called.
- > Is this assumption wrong?
- > If so, how can one force a certain constructor to be called before any
- > others?
- > If not, why does the following program violate this assumption under my
- > Watcom v10.0 compiler?
- >
- > File z.cpp:
- > ////////////////////////////////////////////////////////////////
- ////////////////
- > #include "mylib.h"
- > #include <iostream.h>
- > #include <string.h>
- >
- > class A {
- > public:
- > A();
- > A(char *as);
- > ~A();
- > private:
- > char *s;
- > };
- >
- > A aa("main global non static");
- > static A a("main global static");
- >
- > A::A(char *as)
- > {
- >
- > cout << "A constructor called for " << as << "\n";
- > s = strdup(mylibf1(as));
- > cout << "S is now" << s << "\n";
- > }
- >
- > A::~A()
- > {
- > cout << "A destructor called\n";
- > }
- >
- > main() {
- > A c("main local non static");
- > static A d("main local static");
- > }
- > ////////////////////////////////////////////////////////////////
- ////////////////
- > File mylib.h
- >
- > char *
- > mylibf1(char *as);
- >
- > ////////////////////////////////////////////////////////////////
- ////////////////
- > File mylib.cpp
- >
- > #include "mylib.h"
- > #include <string.h>
- > #include <iostream.h>
- > #include <malloc.h>
- >
- > class B {
- > public:
- > B();
- > ~B();
- > char *libf1(char *as);
- > private:
- > char *s;
- > };
- >
- > static B dummy;
- >
- > B::B()
- > {
- > cout << "Constructor for dummy called.\n";
- > s = strdup("Initial str");
- > }
- >
- > B::~B()
- > {
- > cout << "Destructor for dummy called.\n";
- > free(s);
- > }
- >
- > char *
- > mylibf1(char *as)
- > {
- > return ::dummy.libf1(as);
- > }
- >
- > char *
- > B::libf1(char *as)
- > {
- > char *tmp;
- >
- > tmp = s;
- > s = strdup(as);
- > return tmp;
- > }
- > ////////////////////////////////////////////////////////////////
- ////////////////
- >
- > The program crashes because the constructor for dummy is called after the
- > constructors for class A objects in file z.cpp,although these objects'
- > constructors call (indirectly) member functions of "dummy".
- > Note that if the programs are linked the other way round (e.g. changing
- > z.cpp to a.cpp) causes the constructors to be executed in the right
- > order.
- > The question of how to cause a constructor to be called before anything
- > else (in any translation unit) also interests me because constructor calling
- > sequence determines the sequence in which destructors will be called. In the
- > above example, I would like the constructor for "dummy" to be called first
- > regardless of whether the constructor for class A objects uses "dummy" members
- > or not. The reason for this is that the destructor for A might use "dummy"
- > members, hence requiring "dummy" destructor to be called last.
- >
- > Any help would be appreciated.
- >
- > Stephanos.
- > stheotok@bme.jhu.edu
-
- Hi,
- I read your comment on the problem. I would not write it in
- that manner. However, I feel you should create the static dummy
- object within the mylibf1 function.
-
- // Try this out, it might solve your problem
-
-
- B::B()
- {
- cout << "Constructor for dummy called.\n";
- s = strdup("Initial str");
- }
-
- B::~B()
- {
- cout << "Destructor for dummy called.\n";
- free(s);
- }
-
- char *
- mylibf1(char *as)
- {
- // This is important. Create the static dummy derived
- // from B within the function itself.
- static B dummy;
-
- return ::dummy.libf1(as);
- }
-